#ifndef __VTOR_I2C_H_
#define __VTOR_I2C_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "vtor_elec_module_config.h"

#ifdef __VTOR_I2C__

#define VTOR_I2C_SLAVE_REG_NUM	255


#define VTOR_I2C_REG_NULL				0
#define VTOR_I2C_REG_SLAVE_TX_LEN		2
#define VTOR_I2C_REG_SLAVE_RX_LEN		3
#define VTOR_I2C_REG_SLAVE_TX_BUF		0x10
#define VTOR_I2C_REG_SLAVE_RX_BUF		0x80


typedef enum
{
	VTOR_I2C_STATE_OK       = 0x00U,
	VTOR_I2C_STATE_ERROR    = 0x01U,
	VTOR_I2C_STATE_BUSY     = 0x02U,
	VTOR_I2C_STATE_TIMEOUT  = 0x03U,

	VTOR_I2C_STATE_DEV_ADDR_NAK  = 0x04U,
	VTOR_I2C_STATE_DEV_REG_NAK  = 0x05U,
	VTOR_I2C_STATE_DEV_DATA_NAKE  = 0x06U,
}VTOR_I2C_State;

typedef enum
{
	GPIO_DIR_OUTPUT			= 0x00U,
	GPIO_DIR_INPUT			= 0x01U,
	GPIO_DIR_IT_2EDGE		= 0x02U, // 输入且边沿中断
}GPIO_DirTypeDef;

typedef enum
{
// 所有状态都是针对主机来说
// 例如write，是主机写，那从机就需要执行接收操作
	VTOR_I2C_SLAVE_STATE_NULL = 0,
	VTOR_I2C_SLAVE_STATE_START,
	VTOR_I2C_SLAVE_STATE_WRITE,
	VTOR_I2C_SLAVE_STATE_READ,
	VTOR_I2C_SLAVE_STATE_NACK,
	VTOR_I2C_SLAVE_STATE_STOP,  // 通信结束
	VTOR_I2C_SLAVE_STATE_COMPLETE, // 用户处理完成
}VtorI2cSlaveState;


typedef struct
{

#ifdef __VTOR_I2C_SLAVE__
	uint8_t selfDevAddr; // 从机固有属性，设备地址
	uint8_t regData[VTOR_I2C_SLAVE_REG_NUM]; // 从机固有属性，寄存器数值

	VtorI2cSlaveState slaveState; // slaveState，分析是主机还是从机进行控制

	int16_t sclRisingCnt; // 上升沿个数，用来做出重要判断
	uint8_t serialRegister; // 每次交换的字节
	uint8_t devAddr; // 主机期望访问的从机设备地址
	uint8_t regAddr; // 主机期望访问的从机寄存器地址

#endif
	uint8_t reserve;
}VtorI2c;


void VtorI2c_Delay(uint32_t tick);

#define GPIO_LEVEL_HIGH		1
#define GPIO_LEVEL_LOW		0

#define GPIO_DIR_INPUT		1
#define GPIO_DIR_OUTPUT		0

void VtorI2c_Init(void);
void VtorI2c_Start(VtorI2c* i2c);
void VtorI2c_Stop(VtorI2c* i2c);
void VtorI2c_SendByte(VtorI2c* i2c, uint8_t txd);
uint8_t VtorI2c_ReadByte(VtorI2c* i2c);
uint8_t VtorI2c_WaitAck(VtorI2c* i2c);
void VtorI2c_Ack(VtorI2c* i2c);
void VtorI2c_NAck(VtorI2c* i2c);

void VtorI2c_WriteScl(VtorI2c* i2c, uint8_t level);
void VtorI2c_WriteSda(VtorI2c* i2c, uint8_t level);
uint8_t VtorI2c_ReadSda(VtorI2c* i2c);
uint8_t VtorI2c_ReadScl(VtorI2c* i2c);
void VtorI2c_SetSdaDir(VtorI2c* i2c, uint8_t dir);

uint8_t VtorI2c_ReadMem(VtorI2c* i2c, uint8_t slaveAddress, uint16_t regAddress, uint8_t regAddrLen , uint8_t* buf, uint16_t len);
uint8_t VtorI2c_WriteMem(VtorI2c* i2c, uint8_t slaveAddress, uint16_t regAddress, uint8_t regAddrLen , uint8_t* buf, uint16_t len);

uint8_t VtorI2cMaster_WriteBuffer(VtorI2c* i2c, uint16_t devAddr, uint8_t* txBuf, uint8_t txLen);
uint8_t VtorI2cMaster_ReadBuffer(VtorI2c* i2c, uint16_t devAddr, uint8_t* rxBuf, uint8_t rxLen);


#ifdef __VTOR_I2C_SLAVE__
void VtorI2cSlave_Init(void);

void VtorI2cSlave_WriteSda(VtorI2c* i2c, uint8_t level);
uint8_t VtorI2cSlave_ReadSda(VtorI2c* i2c);
uint8_t VtorI2cSlave_ReadScl(VtorI2c* i2c);
void VtorI2cSlave_SetSdaDir(VtorI2c* i2c, uint8_t dir);

void VtorI2cSlave_SclHandler(VtorI2c* i2c);
void VtorI2cSlave_SdaHandler(VtorI2c* i2c);

uint8_t VtorI2cSlave_WriteBuffer(VtorI2c* i2c, uint8_t* txbuf, uint8_t txLen);
uint8_t VtorI2cSlave_ReadBuffer(VtorI2c* i2c, uint8_t* rxBuf);

// 从机完成回调函数，用于触发信号量，等
void VtorI2cSlave_StopCallback(VtorI2c* i2c);
void VtorI2cSlave_WriteRegCallback(VtorI2c* i2c);
void VtorI2cSlave_ReadRegCallback(VtorI2c* i2c);
#endif


uint8_t VtorI2c_CheckIo(VtorI2c* i2c);
uint8_t VtorI2c_CheckDevice(VtorI2c* i2c, uint8_t addr);
#endif // __VTOR_I2C__

#ifdef __cplusplus
}
#endif

#endif
